package AlexDB.Resolver;

import java.lang.module.ModuleDescriptor.Modifier;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.ServiceLoader;

import AlexDB.Command.IDbCommandType;
import Common.Handle.ClassUtil;

public class ResolveFactory {

    ArrayList<IDbCommandType> _dbCommandTypes = new ArrayList<IDbCommandType>();

    public void RegisterCommandType(IDbCommandType type)
    {
        _dbCommandTypes.add(type);
    }

    public static ResolveFactory initialized()
    {
        var thisObj = new ResolveFactory();
        var types =ClassUtil.getAllClassByInterface(IDbCommandType.class);
        for (var iDbCommandType : types) {
            try {
                if(!java.lang.reflect.Modifier.isAbstract(iDbCommandType.getModifiers()))
                thisObj._dbCommandTypes.add((IDbCommandType)iDbCommandType.getDeclaredConstructor().newInstance());
            } 
            catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                e.printStackTrace();
            }
        }

        return thisObj;
    }

    public Resolve CreateResolve()
    {
        var types = _dbCommandTypes.toArray(new IDbCommandType[_dbCommandTypes.size()]);
        return Resolve.build(types);
    }

}
